package com.yuyou.fn.report.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.report.entity.Charge;
import com.yuyou.fn.report.mapper.ChargeMapper;
import com.yuyou.fn.report.service.IChargeService;
import com.yuyou.fn.report.vo.ChargeQueryVo;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 *
 * Charge 表数据服务层接口实现类
 *
 */
@Service
public class ChargeServiceImpl extends SuperServiceImpl<ChargeMapper, Charge,String> implements IChargeService {


    private final static Logger log  = LoggerFactory.getLogger(ChargeServiceImpl.class);


    @Override
    public Page<Charge> findByQuery(ChargeQueryVo chargeQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        if(chargeQueryVo.getEndDate() != null ){
            chargeQueryVo.setEndDate(DateUtils.addDays(chargeQueryVo.getEndDate() , 1 ));
        }
        Example<Charge> example = Charge.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,chargeQueryVo);
        /*if(dataPermission != null ){
            CriteriaUtils.fillConditionForBusPermission(criteria,dataPermission.getBusTeamIds());
        }*/

        Page<Charge> page = findPageByExample(example, pageNo, pageSize);
        if(page.getRecords() != null ){
            for(Charge charge : page.getRecords() ){
                if(charge.getBusType() == 2 ){//退班退费
                    charge.setRefundAmount(charge.getRefundAmount().multiply(BigDecimal.valueOf(-1)));
                }
            }
        }
        return page ;
    }

    @Override
    public List<Charge> findListByQuery(ChargeQueryVo chargeQueryVo, DataPermission dataPermission) {
        if(chargeQueryVo.getEndDate() != null ){
            chargeQueryVo.setEndDate(DateUtils.addDays(chargeQueryVo.getEndDate() , 1 ));
        }

        Example<Charge> example = Charge.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,chargeQueryVo);
        if (chargeQueryVo.getChargeCampusId()==null){
            List<Long> campusIdList= dataPermission.getCampusIds();
            if (campusIdList!=null && campusIdList.size()>0){
                criteria.andIn("chargeCampusId",campusIdList);
            }else {
                return Collections.EMPTY_LIST;
            }
        }

       /* if(dataPermission != null ){
            CriteriaUtils.fillConditionForBusPermission(criteria,dataPermission.getBusTeamIds());
        }*/

        List<Charge> List = findListByExample(example);
        if(List != null ){
            for(Charge charge :List ){
                if(charge.getBusType() == 2 ){//退班退费
                    charge.setRefundAmount(charge.getRefundAmount().multiply(BigDecimal.valueOf(-1)));
                }
            }
        }
        return List ;
    }

    @Override
    public void changeDailyStatus(List<String> chargeIds ,Integer dailyStatus , Principals.Principal principal ){
        Example<Charge> example = Charge.createExample();
        example.createCriteria().andIn("chargeId" , chargeIds ).andEqualTo("dailyStatus", 0 );
        Charge charge = new Charge();
        charge.setDailyStatus(dailyStatus);
        charge.setDailyOperator(principal.name);
        charge.setDailyOperatorId(principal.id);

        updateSelectiveByExample(charge,example);

    }

    @Override
    public void cancelDailyStatus(List<String> chargeIds, Integer dailyStatus, Principals.Principal principal) {
        Example<Charge> example = Charge.createExample();
        example.createCriteria().andIn("chargeId" , chargeIds ).andEqualTo("dailyStatus", 1 );
        Charge charge = new Charge();
        charge.setDailyStatus(dailyStatus);
        charge.setDailyOperator(principal.name);
        charge.setDailyOperatorId(principal.id);

        updateSelectiveByExample(charge,example);
    }
}